package IC.Semantics.symbols;

import java.util.ArrayList;

import IC.Semantics.types.MethodType;

/**
 * calss to hold symbol table for a CLASS type 
 */
public class ClassSymbolTable extends SymbolTable{

	public ClassSymbolTable(SymbolTable parentTable, String name) 
	{
		super(parentTable, name);
	}
	
	
	public String dump()
	{
		StringBuilder str = new StringBuilder("Class Symbol Table: " + name);
		StringBuilder fields = new StringBuilder();
		StringBuilder methods = new StringBuilder();
		String mt;
		
		for (SemanticSymbol s: entries.values())
		{
			if (s.kind == SemanticKind.FIELD)
				fields.append("\n\tField: " + s.getType().toString() + " " + s.name);
			else 
			{
				mt = (((MethodType)s.type).isStatic() ? "Static" : "Virtual") + " method";
				methods.append("\n\t" + mt + ": " + s.name + " " + s.type.toString() );
			}
		}
		
		str.append(fields);
		str.append(methods);
		
		ArrayList<SymbolTable> mTables = new ArrayList<SymbolTable>();
		ArrayList<SymbolTable> cTables = new ArrayList<SymbolTable>();
		if (!children.isEmpty())
		{
			str.append("\nChildren tables: ");
			
			// save the different types in array list for efficient iteration 
			for (SymbolTable st : children.values())
			{
				if (st instanceof MethodSymbolTable)
					mTables.add(st);
				else
					cTables.add(st);
			}
			
			for (SymbolTable mst : mTables)
				str.append(mst.name + ", ");
			for (SymbolTable cst : cTables)
				str.append(cst.name + ", ");
			str.delete(str.length()-2, str.length());
		}
		
		str.append("\n\n");
		for (SymbolTable mst : mTables)
			str.append(mst.dump());
		
		for (SymbolTable cst : cTables)
			str.append(cst.dump());
		
        return str.toString();
	}


}
